package com.qumap.editview.impl;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.SplitLayoutPanel;
import com.google.gwt.user.client.ui.Widget;
import com.qumap.common.edit.IDisplay;
import com.qumap.editview.gin.EditViewClientModule;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by IntelliJ IDEA.
 * User: Admin
 * Date: 05.05.11
 * Time: 16:13
 * To change this template use File | Settings | File Templates.
 */
public class DisplayImpl implements IDisplay {

	interface DisplayImplUiBinder extends UiBinder<Widget, DisplayImpl> {}

	private static DisplayImplUiBinder ourUiBinder = GWT.create(DisplayImplUiBinder.class);

	private Widget root;

	@Inject
	@Named(EditViewClientModule.MAP_DISPLAY)
	private IDisplay map_display;

	@Inject
	@Named(EditViewClientModule.ACCOUNT_DISPLAY)
	private IDisplay account_display;

	@Inject
	@Named(EditViewClientModule.ITEM_DISPLAY)
	private IDisplay item_display;

	@UiField
	SplitLayoutPanel splitPanel;

	public DisplayImpl() {
		root = ourUiBinder.createAndBindUi(this);
	}

	@Override
	public void setEventBus(EventBus eb) {
		map_display.setEventBus(eb);
		account_display.setEventBus(eb);
		item_display.setEventBus(eb);
	}

	@Override
	public void init() {

		map_display.init();
		account_display.init();
		item_display.init();
		initSplitPanel();
		addComponentsToPanel();
	}

	private void initSplitPanel() {

		Window.addResizeHandler(new ResizeHandler() {
			@Override
			public void onResize(ResizeEvent resizeEvent) {
				int height = resizeEvent.getHeight();
				int width = resizeEvent.getWidth();
				updateSize(width, height);
			}
		});
		updateSize(Window.getClientWidth(), Window.getClientHeight());
	}

	private void updateSize(int width, int height) {
		width-=20;
		height-=20;
		String height_string = height+"px";
		String width_stirng  = width+"px";
		splitPanel.setSize(width_stirng, height_string);

	}


	private void addComponentsToPanel() {
		splitPanel.addWest(map_display.asWidget(), 100);
		splitPanel.addNorth(account_display.asWidget(), 100);
		splitPanel.add(item_display.asWidget());

	}

	@Override
	public void onStop() {
		map_display.onStop();
		account_display.onStop();
		item_display.onStop();
	}

	@Override
	public Widget asWidget() {
		return root;
	}
}